﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NHibernate;

namespace SAKS.ActionFilters
{
    public class NeedsPersistenceAttribute : ActionFilterAttribute
    {

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            MvcApplication.CurrentSession.BeginTransaction();
        }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            var tx = MvcApplication.CurrentSession.Transaction;
            if (tx != null && tx.IsActive)
                tx.Commit();
            base.OnActionExecuted(filterContext);
        }
    }
}